#include "HttpSocket.h"

#define BLOCK_SIZE 1024
#define BLOCK_MASK  0xFFFFFC00 

bool RecvUnknown(int socket);
int Run(char* strUrl);

int main(int args,char *argv[])
{
	//printf("%s\n",argv[1]);
	//char *strUrl=(char*)malloc(sizeof(*argv[1]));
	//strcpy(strUrl,argv[1]);
	//printf("%s\n",strUrl);
	//char* url=argv[1];
	Run(argv[1]);
	return 0;
}

int Run(char *strUrl)
{
	char *strDomain=NULL;
	char *point=NULL;
	char *strWebPath=NULL;
	int port = INTERNET_DEFAULT_HTTP_PORT;
	
	strDomain = strUrl;
	printf("%s\n",strDomain);
	if(strstr(strDomain,"http://") != NULL||strstr(strDomain,"HTTP://")!=NULL)
		strDomain += 7;
	
	else if (strstr(strDomain,"https://") != NULL||strstr(strDomain,"HTTPS://") != NULL)	{
		strDomain += 8;
		port = INTERNET_DEFAULT_HTTPS_PORT;
	}
	
	point = strchr(strDomain,'/');
	strWebPath=(char *)malloc(sizeof(point));
	strcpy(strWebPath,point);
	
	int pos = strchr(strDomain,'/') - strDomain;
	
	strDomain[pos]='\0';

	printf("domain:%s  ,web path:%s\n",strDomain,strWebPath);
	
	int nLength;
	
	//do {		
		CHttpSocket socket;
		//socket.SetDelayTime(0);	
		const char *request_header=socket.FormatRequestHeader(strDomain,strWebPath,nLength);
		free(strWebPath);
		if (!socket.Socket()){
			printf("http socket create failed!\n");
			//break;
			return 1;
		}
		
		//socket.SetTimeout(10000,0);
		
		if (!socket.Connect(strDomain,port)){
			printf("http socket connect failed! server:%s\n",strDomain);
			//break;
			return 1;
		}
		
		if (!socket.SendRequest()){
			printf("http socket send request failed!\nserver:%s\nrequest header:\n%s\n",strDomain,request_header);
			//break;
			return 1;
		}
		
		int m_nStatusCode=socket.GetServerState();
		
		if (m_nStatusCode==-1 || m_nStatusCode!=200)		return 1;//break;
		
		bool m_bSuccess=RecvUnknown(socket.m_s);		
		
	//} while(0);
	
	return 0;
}


bool RecvUnknown(int socket)
{
	unsigned char*    m_pBuffer=NULL; // The block of allocated memory
	unsigned long    m_nLength = 0; // The number of bytes we have written into the block
	unsigned long    m_nBuffer = 0; // The size of the allocated block
	unsigned long    nLength = 4096;
	//DWORD	 m_nOffset;	
	int  ret=0;
	int errsv;
	
	int  try_times=0;
	
	bool bOK=true;

	do {		
		
		if ( m_nBuffer - m_nLength < nLength ) {
			m_nBuffer = m_nLength +  nLength ;
			m_nBuffer = ( m_nBuffer + BLOCK_SIZE - 1 ) & BLOCK_MASK;
			m_pBuffer = (unsigned char*)realloc( m_pBuffer, m_nBuffer ); 
		
			if ( !m_pBuffer ){
				m_nLength = 0;
				m_nBuffer = 0;
			}
		}
			

		ret=recv(socket,(m_pBuffer+m_nLength),4096,0);
		printf("%s",m_pBuffer+m_nLength);
		if (ret==-1)
		{
			errsv=errno;
			if (errsv == EAGAIN)
			{
				if (try_times>10)
				{
					bOK=false;
					break;
				}
				fd_set rfs;
				struct timeval tv;
				FD_ZERO(&rfs);
				FD_SET(socket,&rfs);
				tv.tv_sec = 1;
				tv.tv_usec = 0;
				select(1,&rfs,NULL,NULL,&tv);
				continue;
			}
			
			bOK=false;
			break;
		}
		
		if (ret==0)
		{
			break;
		}
		
		m_nLength+=ret;
		
	} while(true);
	
	return bOK;

}
